package com.ruoyi.web.controller.danger;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.dto.SysDutyDTO;
import com.ruoyi.system.domain.vo.DutyUserCountVO;
import com.ruoyi.system.domain.vo.SysDutyVO;
import com.ruoyi.system.entity.SysDutyUser;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.SysDutyUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 绑定履职负责人(SysDutyUser)表控制层
 * @since 2022-11-15 14:09:01
 */
@RestController
@RequestMapping("sysDutyUser")
public class SysDutyUserController extends BaseController {
    @Resource
    private SysDutyUserService sysDutyUserService;
    @Resource
    private SysDutyUserMapper sysDutyUserMapper;
    @Resource
    private PreventionCheckTaskConfigMapper preventionCheckTaskConfigMapper;
    @Resource
    private PreventionHiddenRegisterMapper preventionHiddenRegisterMapper;
    @Resource
    private PreventionPlanMapper preventionPlanMapper ;

    /**
     * 分页查询
     * @param sysDutyUser 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SysDutyUser sysDutyUser) {
        startPage();
        List<SysDutyUser> sysDutyUsers = sysDutyUserService.queryByPage(sysDutyUser);
        return getDataTable(sysDutyUsers);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        SysDutyUser sysDutyUser = this.sysDutyUserService.queryById(id);
        return AjaxResult.success(sysDutyUser);
    }

    /**
     * 新增数据
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody SysDutyUser sysDutyUser) {
        SysDutyUser insert = sysDutyUserService.insert(sysDutyUser);
        return AjaxResult.success(insert);
    }

    /**
     * 编辑数据
     * @return 编辑结果
     */
    @PostMapping("/update")
//    @PreAuthorize("@ss.hasPermi('sysDutyUser:update')")
    public AjaxResult edit(@RequestBody SysDutyUser sysDutyUser) {
        SysDutyUser query = new SysDutyUser();
        Integer userId = sysDutyUser.getUserId();
        query.setUserId(userId);
        List<SysDutyUser> sysDutyUsers = sysDutyUserService.queryByPage(query);
        if (CollUtil.isNotEmpty(sysDutyUsers)) {
            return AjaxResult.error(500, "该用户已绑定负责人");
        }
        SysDutyUser update = sysDutyUserService.update(sysDutyUser);
        return AjaxResult.success(update);
    }


    /**
     * 包保责任制
     */
    @GetMapping("/toCountSysDuty")
    public AjaxResult toCountSysDuty(String startTime,String endTime) {
        List<DutyUserCountVO> maps = sysDutyUserService.toCountSysDuty(startTime,endTime);
        return AjaxResult.success(maps);
    }





}

